package com.example.ormsqlsplicingframe1.proxy;

import com.example.ormsqlsplicingframe1.utlis.ProxySessionUtils;
import com.example.ormsqlsplicingframe1.utlis.ProxyTransactionListSessionUtils;

import java.util.List;
import java.util.concurrent.atomic.LongAdder;

public class ProxyBaseTransactionSession implements ProxyTransactionListener{

    private static  final  ThreadLocal<LongAdder> threadLocal = ThreadLocal.withInitial(() -> new LongAdder());

    @Override
    public void startTransaction() {
        threadLocal.get().increment();
    }

    @Override
    public void commitTransaction() {

        if (threadLocal.get().intValue() ==0){
            return;
        }

        if (isOpenTransaction()){
            threadLocal.get().decrement();
        }
    }

    @Override
    public void rollbackTransaction() {

        if (threadLocal.get().intValue() ==0){
            return;
        }

        if (isOpenTransaction()){
            threadLocal.get().decrement();
        }
    }

    @Override
    public boolean isOpenTransaction() {
        return threadLocal.get().intValue() > 0 ?  true : false ;
    }
}
